using System.Collections;
using System.Threading;
using OracleInternal.Common;

namespace OracleInternal.Network
{
	internal class DataSources : INamingAdapter
	{
		private static Hashtable _Map;

		private static ReaderWriterLockSlim myLock;

		public string ID => "DataSources";

		public Hashtable Map
		{
			get
			{
				try
				{
					myLock.EnterReadLock();
					return (Hashtable)_Map.Clone();
				}
				finally
				{
					myLock.ExitReadLock();
				}
			}
		}

		static DataSources()
		{
			_Map = ProviderConfig.ConfigDataSourcesMap;
			myLock = new ReaderWriterLockSlim();
		}

		public string Resolve(string TNSAlias, out ConnectionOption CO, string InstanceName = null, string ServerIs = null)
		{
			try
			{
				myLock.EnterReadLock();
				object obj = _Map[TNSAlias];
				CO = null;
				return obj?.ToString();
			}
			finally
			{
				myLock.ExitReadLock();
			}
		}

		public void Refresh()
		{
			try
			{
				myLock.EnterWriteLock();
				ProviderConfig.ReParseDataSourceSection();
			}
			finally
			{
				myLock.ExitWriteLock();
			}
		}
	}
}
